import logging

import logging
from pathlib import Path
from src.downloader import download_sources
from src.validator import validate_sources
from src.processor import process_sources
from src.utils import load_json_file, save_json_file

RAW_SOURCES_PATH = Path("data/input/raw_sources.json")

def setup_logging():
    """配置日志记录"""
    logging.basicConfig(
        level=logging.INFO,
        format="%(asctime)s [%(levelname)s] - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )

def main():
    """主函数，协调整个书源处理流程"""
    setup_logging()
    logging.info("开始处理书源...")

    # 1. 下载书源 (如果文件不存在)
    if not RAW_SOURCES_PATH.exists():
        logging.info("未找到本地原始书源文件，开始下载...")
        raw_sources = download_sources()
        save_json_file(RAW_SOURCES_PATH, raw_sources)
        logging.info(f"下载了 {len(raw_sources)} 个原始书源并保存到 {RAW_SOURCES_PATH}")
    else:
        logging.info(f"从 {RAW_SOURCES_PATH} 加载本地原始书源...")
        raw_sources = load_json_file(RAW_SOURCES_PATH)
        logging.info(f"加载了 {len(raw_sources)} 个原始书源。")


    # 2. 验证书源
    valid_sources, invalid_sources = validate_sources(raw_sources)
    logging.info(f"验证后，得到 {len(valid_sources)} 个有效书源和 {len(invalid_sources)} 个无效书源。")

    # 3. 处理和分类书源
    process_sources(valid_sources)
    logging.info("书源处理和分类完成。")

    logging.info("所有任务已完成。")

if __name__ == "__main__":
    main()
